Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Cookies.js is a small client-side javascript library that makes managing cookies easy.
Features
Browser Compatibility
Getting the Library
Use in CommonJS/Node Environments Without window
A Note About Encoding
API Reference
The following browsers have passed all of the automated Cookies.js tests:
npm install cookies-js
bower install cookies-js
window
In environments where there is no native window
object, Cookies.js will export a factory method
that accepts a window
instance. For example, using jsdom, you
might do something like:
var jsdom = require('jsdom');
var window = jsdom.jsdom().parentWindow;
var Cookies = require('cookies-js')(window);
// Use Cookies as you normally would
RFC6265 defines a strict set of allowed characters for cookie keys and values. In order to effectively allow any character to be used in a key or value, Cookies.js will URI encode disallowed characters in their UTF-8 representation. As such, Cookies.js also expects cookie keys and values to already be URI encoded in a UTF-8 representation when it accesses cookies. Keep this in mind when working with cookies on the server side.
Do not use HttpUtility.UrlEncode and
HttpUtility.UrlDecode on cookie keys or
values. HttpUtility.UrlEncode
will improperly escape space characters to '+'
and lower case every
escape sequence. HttpUtility.UrlDecode
will improperly unescape every '+'
to a space character.
Instead, use
System.Uri.EscapeDataString
and System.Uri.UnescapeDataString.
Methods
Cookies.set(key, value [, options])
Cookies.get(key)
Cookies.expire(key [, options])
Properties
Cookies.enabled
Cookies.defaults
Alias: Cookies(key, value [, options])
Sets a cookie in the document. If the cookie does not already exist, it will be created. Returns the Cookies
object.
Option | Description | Default |
---|---|---|
path | A string value of the path of the cookie | "/" |
domain | A string value of the domain of the cookie | undefined |
expires | A number (of seconds), a date parsable string, or a Date object of when the cookie will expire | undefined |
secure | A boolean value of whether or not the cookie should only be available over SSL | false |
A default value for any option may be set in the Cookies.defaults
object.
Example Usage
// Setting a cookie value
Cookies.set('key', 'value');
// Chaining sets together
Cookies.set('key', 'value').set('hello', 'world');
// Setting cookies with additional options
Cookies.set('key', 'value', { domain: 'www.example.com', secure: true });
// Setting cookies with expiration values
Cookies.set('key', 'value', { expires: 600 }); // Expires in 10 minutes
Cookies.set('key', 'value', { expires: '01/01/2012' });
Cookies.set('key', 'value', { expires: new Date(2012, 0, 1) });
Cookies.set('key', 'value', { expires: Infinity });
// Using the alias
Cookies('key', 'value', { secure: true });
Alias: Cookies(key)
Returns the value of the most locally scoped cookie with the specified key.
Example Usage
// First set a cookie
Cookies.set('key', 'value');
// Get the cookie value
Cookies.get('key'); // "value"
// Using the alias
Cookies('key'); // "value"
Alias: Cookies(key, undefined
[, options])
Expires a cookie, removing it from the document. Returns the Cookies
object.
Option | Description | Default |
---|---|---|
path | A string value of the path of the cookie | "/" |
domain | A string value of the domain of the cookie | undefined |
A default value for any option may be set in the Cookies.defaults
object.
Example Usage
// First set a cookie and get its value
Cookies.set('key', 'value').get('key'); // "value"
// Expire the cookie and try to get its value
Cookies.expire('key').get('key'); // undefined
// Using the alias
Cookies('key', undefined);
A boolean value of whether or not the browser has cookies enabled.
Example Usage
if (Cookies.enabled) {
Cookies.set('key', 'value');
}
An object representing default options to be used when setting and expiring cookie values.
Option | Description | Default |
---|---|---|
path | A string value of the path of the cookie | "/" |
domain | A string value of the domain of the cookie | undefined |
expires | A number (of seconds), a date parsable string, or a Date object of when the cookie will expire | undefined |
secure | A boolean value of whether or not the cookie should only be available over SSL | false |
Example Usage
Cookies.defaults = {
path: '/',
secure: true
};
Cookies.set('key', 'value'); // Will be secure and have a path of '/'
Cookies.expire('key'); // Will expire the cookie with a path of '/'
1.2.3
FAQs
Client-Side Cookie Manipulation API
The npm package cookies-js receives a total of 59,107 weekly downloads. As such, cookies-js popularity was classified as popular.
We found that cookies-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.